package com.example.forumsystem.demos.web.service.impl;

import com.example.forumsystem.demos.web.common.ResultCode;
import com.example.forumsystem.demos.web.exception.ApplicationException;
import com.example.forumsystem.demos.web.mapper.MessageInfoMapper;
import com.example.forumsystem.demos.web.model.MessageInfo;
import com.example.forumsystem.demos.web.model.Result;
import com.example.forumsystem.demos.web.model.UserInfo;
import com.example.forumsystem.demos.web.service.IMessageService;
import com.example.forumsystem.demos.web.service.IUserService;
import com.example.forumsystem.demos.web.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    @Autowired
    private MessageInfoMapper messageInfoMapper;
    @Autowired
    private IUserService userService;

    @Override
    public void create(MessageInfo message) {
        if (message == null || message.getPostUserId() == null || message.getReceiveUserId() == null
                || StringUtil.isEmpty(message.getContent())) {
            log.warn("传来的参数不完整，message: " + message + ", 发帖者的用户id: " + message.getPostUserId()
                    + ", 接收者的用户id: " + message.getReceiveUserId() + ", 发送的内容" + message.getContent());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        UserInfo user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() == 1) {
            log.warn("用户不存在");
        }

        // 设置默认值
        message.setState((byte) 0);//表示未读状态
        message.setDeleteState((byte) 0);
        // 设置创建与更新时间
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        int row = messageInfoMapper.insertSelective(message);
        if (row != 1) {
            log.warn("发送信息失败");
        }
    }

    @Override
    public Integer selectUnreadCount(Long id) {
        if (id == null || id < 0){
            log.info("参数校验失败，传来的id为: " + id);
        }
        return messageInfoMapper.selectUnreadCount(id);
    }

    @Override
    public List<MessageInfo> selectByReceiveUserId(Long receiveUserId) {
        if (receiveUserId == null || receiveUserId <= 0) {
            log.warn("接受者id有误，为: " + receiveUserId);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        return messageInfoMapper.selectByReceiveUserId(receiveUserId);
    }

    @Override
    public MessageInfo selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn("参数校验失败, 传来的站内信id为: " + id);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if (id == null || id <= 0 || state < 0 || state > 2) {
            log.warn("传来的消息id错误, 为: " + id);
            return;
        }

        if (state < 0 || state > 2){
            log.info("要设置的状态是错误的，只能为0-2，传来的是: " + state);
            return;
        }

        MessageInfo updateMessage = new MessageInfo();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date = new Date();
        updateMessage.setUpdateTime(date);

        int row = messageInfoMapper.updateByPrimaryKeySelective(updateMessage);
        if (row != 1) {
            log.warn("更新失败");
        }
    }

    @Override
    public void reply(Long repliedId, MessageInfo message) {
        if (repliedId == null || repliedId <= 0) {
            log.info("传来的回复信息id错误, 为: " + repliedId);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        MessageInfo existsMessage = messageInfoMapper.selectByPrimaryKey(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            log.warn("要回复的消息不存在，或者已经被删除");
        }

        updateStateById(repliedId, (byte) 2);

        create(message);  //将回复的内容写入数据库
    }

}
